﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace StoveTop
{
	[Activity (Label = "RecipeListScreen")]
	public class RecipeListScreen : Activity{
		StoveTop.Models.Recipe[] items;
		List<StoveTop.Models.Recipe> recipeNames;

		protected override void OnCreate(Bundle bundle){
			base.OnCreate (bundle);

			SetContentView (Resource.Layout.RecipeListScreen);



			StoveTopDBHelper db = new StoveTopDBHelper (this);
			recipeNames = db.GetAllRecipes();
			items = recipeNames.ToArray();

			ListView list = FindViewById<ListView> (Resource.Id.rListView);

			list.Adapter = new RecipeListAdapter (this, items);

			//Brings user to a selected recipe's Recipe Screen
			list.ItemClick += delegate (object sender, Android.Widget.AdapterView.ItemClickEventArgs args){
				var recipeScreen = new Intent (this, typeof(StoveTop.Screens.StoveTopRecipeScreen));
				recipeScreen.PutExtra ("recipeId", items [args.Position].GetId ());
				StartActivity (recipeScreen);
			};


			EditText field = FindViewById<EditText> (Resource.Id.querry);

			//Searches List of Recipes by name
			Button searchName = FindViewById<Button> (Resource.Id.nameSearch);
			searchName.Click += delegate {
				recipeNames = db.SearchForRecipes(field.Text);
				items = recipeNames.ToArray();
				list.RefreshDrawableState();
				list.Adapter = new RecipeListAdapter (this, items);
			};


			//Searches List of Recipes by tag
			Button searchCat = FindViewById<Button> (Resource.Id.catSearch);
			searchCat.Click += delegate {
				recipeNames = db.GetAllRecipesWithTag(field.Text);
				items = recipeNames.ToArray();
				list.RefreshDrawableState();
				list.Adapter = new RecipeListAdapter (this, items);
			};

		}
	}




	public class RecipeListAdapter : BaseAdapter<string> {
		StoveTop.Models.Recipe[] items;
		Activity context;
		public RecipeListAdapter(Activity context, StoveTop.Models.Recipe[] items) : base() {
			this.context = context;
			this.items = items;
		}
		public override long GetItemId(int position)
		{
			return position;
		}
		public override string this[int position] {  
			get { return items[position].GetName(); }
		}
		public override int Count {
			get { return items.Length; }
		}
		public override View GetView(int position, View convertView, ViewGroup parent)
		{
			View view = convertView; // re-use an existing view, if one is available
			if (view == null) // otherwise create a new one
				view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);
			view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position].GetName();
		
			string secText = "";
			decimal rating = items [position].GetRating ();
			decimal difficulty = items [position].GetDifficulty ();
			secText += "Rating: ";
			for (int i = 0; i < rating; i++)
				secText += "*";
			secText += "\tDifficulty: ";
			for (int i = 0; i < difficulty; i++)
				secText += "*";



			view.FindViewById<TextView> (Android.Resource.Id.Text2).Text = secText;
			return view;
		}
	}
}

/*public class RecipeList : ListActivity 
{
	StoveTop.Models.Recipe[] items;
	List<StoveTop.Models.Recipe> recipeNames;

	protected override void OnCreate(Bundle bundle)
	{
		StoveTopDBHelper db = new StoveTopDBHelper (this);
		recipeNames = db.GetAllRecipes();
		base.OnCreate(bundle);
		items = recipeNames.ToArray();
		ListAdapter = new RecipeListAdapter(this, items);
	}
	protected override void OnListItemClick(ListView l, View v, int position, long id)
	{
		var recipeScreen = new Intent (this, typeof(StoveTop.Screens.StoveTopRecipeScreen));
		recipeScreen.PutExtra ("recipeId", items [position].GetId ());
		StartActivity (recipeScreen);
	}*

}*/